<template>
  <a-modal title="用户管理" :width="640" :visible="visible" :confirmLoading="confirmLoading" :maskClosable="false" @ok="handleSubmit"
           @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="小程序">
          <a-select showSearch optionFilterProp="children" :filterOption="filterOption" :disabled="isDisabled"
                    v-decorator="['parentCode', {rules: [{required: true, message: '必输'}]}]">
            <a-select-option v-for="item in xcxlist" :key="item.code">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="登录账户">
          <a-input v-decorator="[ 'username', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="密码">
          <a-input type="password" v-decorator="[ 'password', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group buttonStyle="solid"
                         v-decorator="[ 'status', {rules: [{ required: true, message: '请选择' }], initialValue: true}]">
            <a-radio-button :value="true">启用</a-radio-button>
            <a-radio-button :value="false">禁用</a-radio-button>
          </a-radio-group>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="真实姓名">
          <a-input v-decorator="['realName', {rules: [{ required: true, message: '请选择' }] }]"/>
        </a-form-item>

        <a-divider/>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="赋予权限"
        >
          <a-select mode="multiple" placeholder="菜单权限"
                    v-decorator="[ 'roleIds', {rules: []} ]">
            <a-select-option v-for="item in roleList" :key="item.id">{{item.roleName}}</a-select-option>
          </a-select>
        </a-form-item>

<!--        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="拥有角色">-->
<!--          <a-select v-decorator="[ 'roleId', {rules: [{ required: true, message: '请选择' }]} ]">-->
<!--            <a-select-option v-for="item in roleList" :key="item.id">{{item.name}}</a-select-option>-->
<!--          </a-select>-->
<!--        </a-form-item>-->

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate, optionselect1 } from '@/api/system/user'
  import { optionselect } from '@/api/system/role'

  /**
   * 数据
   */
  const data = function() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      id: null,
      visible: false,
      confirmLoading: false,
      isDisabled: false,
      roleList: [],
      xcxlist: [],
      form: this.$form.createForm(this)
    }
  }

  const created = function() {
    optionselect().then(res => {
      this.roleList = res.data
    })
    optionselect1().then(res => {
      this.xcxlist = res.data
    })
  }

  export default {
    props: {
      companys: {
        type: Array,
        default: null
      }
    },
    data: data,
    created: created,
    /**
     * 方法
     */
    methods: {
      add () {
        this.visible = true
      },
      edit (record) {
        this.visible = true
        this.id = record.id
        this.isDisabled = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['parentCode', 'username', 'password', 'status', 'roleIds', 'realName'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            values.userType = 2 //代理商用户
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.id = null
        this.visible = false
        this.isDisabled = false
        this.confirmLoading = false
        this.form.resetFields()
      },
      filterOption (input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },

    },
  }
</script>
